<template>
  <div class="mod-sys__images">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.filename" placeholder="名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <ren-select v-model="state.dataForm.imageType" dict-type="image_type" placeholder="图片类型"></ren-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.updatedAt" placeholder="更新时间" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:images:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('sys:images:delete')" type="danger" @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" label="名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="url" label="url" header-align="center" align="center">
        <template v-slot="scope">
          <small-image :style="`--my-width: ${50}px; --my-height: ${50}px;`" :url="scope.row.url" />
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" header-align="center" align="center"></el-table-column>
      <el-table-column prop="size" label="大小" header-align="center" align="center"></el-table-column>
      <el-table-column prop="imageType" label="图片类型" header-align="center" align="center">
        <template v-slot="scope">
          {{ state.getDictLabel("image_type", scope.row.imageType) }}
        </template>
      </el-table-column>
      <el-table-column prop="isActive" label="启用状态" header-align="center" align="center">
        <template v-slot="scope">
          {{ state.getDictLabel("is_active", scope.row.isActive) }}
        </template>
      </el-table-column>
      <el-table-column prop="isRecommend" label="推荐状态" header-align="center" align="center">
        <template v-slot="scope">
          {{ state.getDictLabel("is_recommend", scope.row.isRecommend) }}
        </template>
      </el-table-column>
      <el-table-column prop="imageBelong" label="归属类型" header-align="center" align="center">
        <template v-slot="scope">
          {{ state.getDictLabel("image_belong", scope.row.imageBelong) }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="belongId" label="归属id" header-align="center" align="center"></el-table-column> -->
      <el-table-column prop="updatedAt" label="更新时间" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('sys:images:update')" type="primary" link @click="addOrUpdateHandle(scope.row.imageId)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('sys:images:delete')" type="primary" link @click="state.deleteHandle(scope.row.imageId)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./images-add-or-update.vue";
import SmallImage from "../../components/small-image/index.vue";

const view = reactive({
  getDataListURL: "/sys/images/page",
  getDataListIsPage: true,
  exportURL: "/sys/images/export",
  deleteURL: "/sys/images",
  deleteIsBatch: true,
  dataForm: {
    filename: "",
    imageType: "",
    updatedAt: "",
    orderField: "update_date",
    order: "desc"
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });


const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (imageId?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(imageId);
  });
};
</script>